<?
require_once 'config.php';
   $username = getValue('username', 'str','POST','',3);
   $pass		        =  getValue('password', 'str', 'POST', '',3);
   $config_password =  getValue('password_again','str','POST','',3);
   $use_security	  =  rand(111111,999999);
   $password        =  md5($pass . $use_security);
   $use_date		  =  time();
   $use_active		  =	1;
   $use_group	  =	1;
   $myform = new generate_form();
   $myform->add('use_name','username',0,1,'',1,'Lỗi nhập email',1,'Trùng email');
   $myform->add('use_password', 'password', 0, 1, '', 1, 'Bạn chưa nhập mật khẩu.', 0, '');
   $myform->add('use_phone','phone',0,0,'',0,'Bạn chưa nhập số điện thoại',0);
   $myform->add('use_firstname','firstname',0,0,'',0,'Bạn chưa nhập họ',0);
   $myform->add('use_lastname','lastname',0,0,'',0,'Bạn chưa nhập tên',0);
   $myform->add('use_contact','address',0,0,'',0,'',0);
   $myform->add('use_date', 'use_date', 1, 1, 0, 0, '', 0, '');
   $myform->add('use_security', 'use_security', 1, 1, '', 0, '', 0, '');
   $myform->add('use_active', 'use_active', 1, 1, 0, 0, '', 0, '');
   $myform->add('use_group', 'use_group', 1, 1, 0, 0, '', 0, '');
   
   $myform->addTable('users');
   
   $myform->evaluate();
   $action	= getValue('action_res', 'str', 'POST', '');
?>
<!DOCTYPE html>
<html lang="en">
<head>	
	<title>Đăng ký</title>
	<meta name="google-site-verification" content="gkB9RQiF61Fg779oj1do-N0_IcXutfYartJIAF7Dbtw" />
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta property="og:image" content=""/>
   <meta name="description" content="" />
   <meta name="keywords" content="" />
	<link type="image/x-icon" href="/favicon_home.ico" rel="icon" />
	<link rel="SHORTCUT ICON" href="/favicon_home.ico"/>   
   <link rel="stylesheet" type="text/css" href="../themes/css/base.css"/>
   <link rel="stylesheet" type="text/css" href="../themes/css/login.css"/>
   
   <script type="text/javascript" src="../themes/js/jquery.js"></script>
    <?=$analytics?>
</head>
<body>
   <div class="globalContainer">
      <?
         include('view/common/vic_header.php');
      ?>
      <div class="line"></div>
      <div class="massseo_login w1136">
         <div class="login_wrap">
            <div class="login">
               <p class="login_title">ĐĂNG KÝ</p>
               <?
                  if($action			==	'action_res'){
                      $captcha = getValue('captcha','str','POST','');
                      if($captcha == $_SESSION["securitycode"]) {
                          if($config_password == $pass){
                      		$errorMsg_res		.=	$myform->checkdata();
                      		 if($errorMsg_res == ''){
                      		 	$db_ex		=	new db_execute_return();
                      			$last_id		=	$db_ex->db_execute($myform->generate_insert_SQL());
                      			if($last_id	> 0){
                      				$myuser = new user($username, $password);
                      				$myuser->savecookie(time() + (365 * 86400));
                                      $link_home = '"/"';
                      				echo "<script>alert('Bạn đã đăng ký thành công !','Thông báo',(setTimeout('window.location.href=".$link_home."', 3000)));</script>";
                      			}
                                  else {
                                      echo "<script>alert('Có lỗi xảy ra !')</script>";
                                  }
                      		 }else echo $errorMsg_res;
                       	}
                      }
                      else {
                          echo "<script>alert('Mã bảo mật không đúng !')</script>";        
                      }
                  	
                  }
               ?>
               <form action="" method="post" onsubmit="return validate_form_res();">
                  <input type="hidden" name="action_res" value="action_res" />
                  <div class="re_one">
                     <p>Email *</p>
                     <div class="input_wrap flr">
                        <div class="input_inner">
                           <input name="username" id="username" placeholder="Email"/>
                        </div>
                     </div>
                  </div>
                  <div class="re_one">
                     <p>Mật khẩu *</p>
                     <div class="input_wrap flr">
                        <div class="input_inner">
                           <input type="password" name="password" id="password" placeholder="Mật khẩu phải từ 6 ký tự trở lên"/>
                        </div>
                     </div>
                  </div>
                  <div class="re_one">
                     <p>Nhập lại mật khẩu *</p>
                     <div class="input_wrap flr">
                        <div class="input_inner">
                           <input type="password" name="password_again" id="config_password" placeholder=""/>
                        </div>
                     </div>
                  </div>
                  <div class="re_one">
                     <p>Họ </p>
                     <div class="input_wrap flr" style="width: 220px;margin-right: 200px;">
                        <div class="input_inner" style="width: 200px">
                           <input name="firstname" placeholder="" style="width: 185px"/>
                        </div>
                     </div>
                  </div>
                  <div class="re_one">
                     <p>Tên </p>
                     <div class="input_wrap flr" style="width: 220px;margin-right: 200px;">
                        <div class="input_inner" style="width: 200px">
                           <input name="lastname" placeholder="" style="width: 185px"/>
                        </div>
                     </div>
                  </div>
                  <div class="re_one">
                     <p>Điện thoại </p>
                     <div class="input_wrap flr" style="width: 220px;margin-right: 200px;">
                        <div class="input_inner" style="width: 200px" >
                           <input name="phone" placeholder="" style="width: 185px"/>
                        </div>
                     </div>
                  </div>
                  <div class="re_one">
                     <p>Địa chỉ</p>
                     <div class="input_wrap flr">
                        <div class="input_inner">
                           <input name="address" placeholder=""/>
                        </div>
                     </div>
                  </div>
                  <div class="re_one">
                     <p>Mã xác nhận</p>     
                                    
                     <div class="input_wrap flr" style="width: 300px;margin-right: 123px;">
                        <img src="/home/securitycode.php" />
                        <div class="input_inner" style="width: 200px">
                           <input  name="captcha" placeholder="" style="width: 185px"/>
                        </div>
                     </div>
                     
               </div>
                  <div class="login_submit_wrap">
                     <input type="submit" class="login_submit" value="Đăng ký" />
                  </div>
               </form>
            </div>
         </div>
      </div>
      <div class="line"></div>
      <?
         include('view/common/vic_footer.php');
      ?>
   </div>
</body>
</html>
<script>
	function validate_form_res(){       	
       	$password = document.getElementById("password").value;
       	$config_password = document.getElementById("config_password").value;
       	$txtusername = document.getElementById("username").value;
    		if($txtusername == null || $txtusername == ""){
       		alert("Vui lòng nhập email đăng ký !");
       		document.getElementById("username").style.border='solid 1px #FF0000'; 
    			document.getElementById("username").focus(); return false;
       	}else{
       		document.getElementById("username").style.border='solid 1px #00FF00'; 
       	}
    		if($password == null || $password == "" || $password.length < 6){
       		alert("Vui lòng nhập lại mật khẩu đăng ký !");
       		document.getElementById("password").style.border='solid 1px #FF0000'; 
    			document.getElementById("password").focus(); return false;
       	}else{
       		document.getElementById("password").style.border='solid 1px #00FF00'; 
       	}
       	
       	if($password != $config_password){
       		alert("Nhập lại mật khẩu chưa đúng !");
       		document.getElementById("config_password").style.border='solid 1px #FF0000'; 
    			document.getElementById("config_password").focus(); return false;
       	}else{
       		document.getElementById("config_password").style.border='solid 1px #00FF00'; 
       	}
       	
    		if($config_password == null || $config_password == ""){
       		alert("Vui lòng nhập lại password đăng ký !");
       		document.getElementById("config_password").style.border='solid 1px #FF0000'; 
    			document.getElementById("config_password").focus(); return false;
       	}else{
       		document.getElementById("config_password").style.border='solid 1px #00FF00'; 
       	}
       	
       	
	}
    //$('.signup').click(function() { 
//        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
//        var emailaddressVal = $("#email").val();
//        if(emailaddressVal == '') {
//            $(".alert_email").html('');
//            $(".alert_email").html('Vui lòng nhập email của bạn !');
//            return false;
//        }
//        else if(!emailReg.test(emailaddressVal)) {
//            $(".alert_email").html('');
//            $(".alert_email").html('Vui lòng nhập 1 email hợp lệ !');
//            return false;
//        }
//        else return true;
//    });
</script>